# 作者：lian
import re

def use_greedy():
    s = "This is a number 234-235-22-423"
    ret = re.match(r".+?(\d+-\d+-\d+-\d+)",s)
    print(ret.group(1))
    print(re.match(r"aa(\d+)","aa2343ddd").group(1))
    print(re.match(r"aa(\d+?)","aa2343ddd").group(1))
    print(re.match(r"aa(\d+)ddd","aa2343ddd").group(1))
    print(re.match(r"aa(\d+?)ddd","aa2343ddd").group(1))

def use_r():
    mm = "c:\\a\\b\\c"
    print(mm)
    print(re.match(r"c:\\",mm).group())


def single():
    """
    匹配单个字符

    """
    ret = re.match(".","M")
    print(ret.group())

    ret = re.match("t.o","too")
    print(ret.group())

    ret = re.match("t.o","two")
    print(ret.group())

def more_alp():
    ret = re.match("[A-Z][a-z]*","M")
    print(ret.group())

    ret = re.match("[A-Z][a-z]*","MnnM")
    print(ret.group())

    ret = re.match("[A-Z][a-z]*","Aabcdef")
    print(ret.group())

    names = ["name1","_name","2_name","__name__"]
    for name in names:
        ret = re.match('[a-zA-Z_]w*',name)
        if ret:
            print(f'{name}是正确的')
        else:
            print(f'{name}不符合命名规范')

    print('-'*50)
    ret = re.match(r"[1-9]?[0-9]","7")
    print(ret.group())

    print('-'*50)
    ret = re.match("[a-zA-Z0-9_]{6}","12a3g45678")
    print(ret.group())



def start_end():
    """
    匹配结尾
    """
    ret = re.match(r"[1-9]?\d$|100","7")
    if ret:
        print(ret.group())
    else:
        print('不是0-99之间')

    #匹配1-99
    ret = re.match(r"[1-9]|[1-9][0-9]","11")
    if ret:
        print(ret.group())
    else:
        print('不是1-99之间')
    print('-'*50)
    ret = re.match(r"\w{4,20}@(163|126|qq)\.com","test@qq.com")
    print(ret.group())
    ret = re.match(r"\w{4,20}@(163|126|qq)\.com","test@126.com")
    print(ret.group())
    print('-'*50)
    # ret = re.match("([^-]+)-(\d+)","010-12345678")

def number_generator(start = 0):
    while start<=5:
        yield start
        start += 1
    return

def use_generator():
    gen = number_generator()
    print(next(gen))
    print(next(gen))
    print(next(gen))


def use_generator():
    gen = number_generator()
    # print(next(gen))
    # print(next(gen))
    # print(next(gen))
    for i in gen:
        print(i)

def find_second_match(pattern,test):
    """
    迭代器
    """
    matches = re.finditer(pattern,test)
    try:
        next(matches)
        next(matches)
        next(matches)
        second_match = next(matches)
        return second_match.group()
    except StopIteration:
        return None
def find_all():
     s = 'hello world, now is 2020/7/20 18:48, 现在是 2020年7 月20日18时48分。'
     ret_s = re.sub(r'年|月', r'/', s)
     ret_s = re.sub(r'日', r' ', ret_s)
     ret_s = re.sub(r'时|分', r':', ret_s)
     print(ret_s)
     # findall 有问题
     pattern = re.compile(r'\d{4}/[01]?[0-9]/[1-3]?[0-9]\s(0[0-9]|1[0-9]|2[0-4])\:[0-5][0-9]')
     ret = pattern.findall(ret_s)
     print(ret)

     ret1 = pattern.search(ret_s)
     print(ret1.group())

def use_sub():
    long_text = """
    <div>
 <p>岗位职责：</p>
 <p>完成推荐算法、数据统计、接口、后台等服务器端相关工作</p>
 <p><br></p>
王道码农训练营-WWW.CSKAOYAN.COM
王道码农训练营-WWW.CSKAOYAN.COM
 <p>必备要求：</p>
 <p>良好的自我驱动力和职业素养，工作积极主动、结果导向</p>
 <p>&nbsp;<br></p>
 <p>技术要求：</p>
 <p>1、一年以上 Python 开发经验，掌握面向对象分析和设计，了解设计模式</p>
 <p>2、掌握HTTP协议，熟悉MVC、MVVM等概念以及相关WEB开发框架</p>
 <p>3、掌握关系数据库开发设计，掌握 SQL，熟练使用 MySQL/PostgreSQL 中的一种<
 br></p>
 <p>4、掌握NoSQL、MQ，熟练使用对应技术解决方案</p>
 <p>5、熟悉 Javascript/CSS/HTML5，JQuery、React、Vue.js</p>
 <p>&nbsp;<br></p>
 <p>加分项：</p>
 <p>大数据，数理统计，机器学习，sklearn，高性能，大并发。</p>
 </div>
    """
    print(long_text)
    ret = re.sub(r'<[^>]*>|&nbsp;|\n|\s','',long_text)
    print(ret)
def other_func():
    ret = re.search(r"\d+","阅读次数是9999,点赞888")
    print(ret.group())
    print('-'*50)
    ret = re.findall(r"\d+","python = 9999,c = 7890,c++ = 12345")
    print(ret)
    print('-' * 50)
    ret = re.sub(r"\d+", '998', "python = 997")
    print(ret)
    ret = re.sub(r"\d+", lambda x : str(int(x.group())+100), "python = 997")
    print(ret)
    ret = re.sub(r"\d+",add,"python=997")
    print(ret)
    print('-' * 50)
    text = "apple apple apple apple"
    pattern = r"apple"
    replacement = "orange"

    new_text = re.sub(pattern, replacement, text, count=3)
    print(new_text)

def add(temp):
     strNum = temp.group()
     num = int(strNum) + 1
     return str(num)




if __name__ == '__main__':
    # single()
    # more_alp()
    # other_func()
    # start_end()
    # text = "abc123def456ghi789"
    # pattern = r"\d+"
    # second_match = find_second_match(pattern, text)
    # print(second_match)
    # print(gen)
    # find_all()
    # use_sub()
    use_r()